﻿using System;
using System.Net;
using System.Text;

using zijian666.Converts.Abstractions;
using zijian666.Converts.Extensions;

namespace zijian666.Converts.Translator;

/// <summary>
/// 字符串翻译器
/// </summary>
public class StringTranslator : ITranslator
{

    /// <inheritdoc />
    public bool CanTranslate(Type type) =>
          typeof(IFormattable).IsAssignableFrom(type)
          || type == typeof(StringBuilder)
          || type == typeof(Uri)
          || type == typeof(IPAddress);

    /// <inheritdoc />
    public object? Translate(IConvertContext context, object input)
        => input switch
        {
            StringBuilder stringBuilder => stringBuilder.ToString(),
            Uri uri => uri.ToString(),
            IPAddress ipAddress => ipAddress.ToString(),
            IFormattable formattable => context.Settings.Format(formattable),
            _ => null,
        };
}
